<?php

namespace draw;

// 意间绘画
// https://www.showdoc.com.cn/2114756190905892/9758916607849845
class Yjai
{
    // 环境：http://api.yjai.art:8080。
    const API_URL = 'http://api.yjai.art:8080/painting-open-api/site/';
    private $AK;
    private $SK;
    private $timestamp;
    public function __construct($option = array()) {
        $this->AK = $option['AK'];
        $this->SK = $option['SK'];
        $this->timestamp = time();
    }

    // 开始绘画
    public function put_task($prompt,$ratio,$style,$guidence_scale,$engine,$callback_url,$callback_type,$enable_face_enhance,$is_last_layer_skip,$init_image,$init_strength){
        $url = self::API_URL."put_task";
        $data = array(
            'apikey'=>$this->AK,
            'timestamp'=>$this->timestamp,
            'prompt'=>$prompt,
            'ratio'=>$ratio,
            'style'=>$style,
            'guidence_scale'=>$guidence_scale,
            'engine'=>$engine,
            'callback_url'=>$callback_url,
            'callback_type'=>$callback_type,
            'enable_face_enhance'=>$enable_face_enhance,
            'is_last_layer_skip'=>$is_last_layer_skip,
            'init_image'=>$init_image,
            'init_strength'=>$init_strength
        );
        $n = $this->http_post($url,$data);
        return $n;
    }
    // 获取任务详情
    public function show_task($uuid){
        $url = self::API_URL."show_task_detail";
        $data = array(
            'apikey'=>$this->AK,
            'timestamp'=>$this->timestamp,
            'uuid'=>$uuid,
        );

        $n = $this->http_post($url,$data);
        return $n;
    }
    // 获取完成的任务
    public function show_complete_tasks($uuid){
        $url = self::API_URL."show_complete_tasks";
        $data = array(
            'apikey'=>$this->AK,
            'timestamp'=>$this->timestamp
        );
        $n = $this->http_post($url,$data);
        return $n;
    }

    // 获取风格画家
    public function get_style_list(){
        $url = self::API_URL."get_draw_selector4";
        $data = array(
            'apikey'=>$this->AK,
            'timestamp'=>$this->timestamp
        );
        $n = $this->http_post($url,$data);
        return $n;
    }

    // 获取用户信息
    public function getUserInfo(){
        // return $this->getsign();
        $url = self::API_URL."getUserInfo";
        $data = array(
            'apikey'=>$this->AK,
            'timestamp'=>$this->timestamp,
        );
        $n = $this->http_post($url,$data);
        return $n;
    }
    private function getsign($data=array()) {
        $arr = array_merge(array('apisecret'=>$this->SK),$data);
        ksort($arr);
        foreach ($arr as $key => $val) {
            $param[] = $key . "=" .($val);
        }
        $sparam = join("&", $param);
        // return $sparam;
        $s = md5($sparam);
        return $s;
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    private function http_post($url, $param, $jsonpost = false, $post_file = false) {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        $httpHeaders = array("Content-Type:application/x-www-form-urlencoded","sign:".$this->getsign($param));


        if ($jsonpost) {
            $strPOST = str_replace('\/', '/', json_encode($param));
        } elseif (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" .($val);
            }
            $strPOST = join("&", $aPOST);
        }

        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, $httpHeaders);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);

        $sContent = curl_exec($oCurl);
        $sContent = json_decode($sContent,true);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        return $sContent;
    }

    public function getError() {
        return $this->error;
    }
}